package com.EJ.ejcompanyweb;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.EJ.ejcompanyweb.View.ViewEntity.ImageBannerEntry;
import com.EJ.ejcompanyweb.adapter.ViewPagerAdapter;
import com.EJ.ejcompanyweb.entity.ItemEntity;
import com.EJ.ejcompanyweb.updata.UpdataAsyncTask;
import com.EJ.ejcompanyweb.updata.UpdataThread;
import com.google.android.material.tabs.TabLayout;
import com.kelin.banner.BannerEntry;
import com.kelin.banner.view.BannerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private  String TAG=this.getClass().getSimpleName();
    private ArrayList<ItemEntity> itemEntities = new ArrayList<ItemEntity>();
    BannerView bannerView;
    public static Handler MAINACTIVITY_HANDLER = new Handler(Looper.getMainLooper());
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        //setContentView(R.layout.activity_main_view);
        setContentView(R.layout.activity_main);
        GetUpdata();
        //拿到数据源
        itemEntities= (ArrayList<ItemEntity>) getIntent().getSerializableExtra("itemEntities");
        FragmentManager fragmentManager=getSupportFragmentManager();

        ViewPager mViewPager=findViewById(R.id.main_viewpager);

        mViewPager.setAdapter(new ViewPagerAdapter(fragmentManager,itemEntities));
        TabLayout mTabLayout=findViewById(R.id.main_tab_layout);
        mTabLayout.setupWithViewPager(mViewPager);
//

//
        findViewById(R.id.editText).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StartActivity();
            }
        });
        findViewById(R.id.imageView3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StartActivity();
            }
        });
        initBannerView();
    }
    private void StartActivity() {
        SearchActivity.activityStart(this);
    }

    private void  initBannerView(){
        bannerView=findViewById(R.id.BannerView);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // bannerView.setShowLeftAndRightPage(30, true, new GalleryTransformer());
        } else {
            bannerView.setShowLeftAndRightPage(20);
        }
        //设置自定义翻页动画改变器，也可以不设置。如果没有设置，则使用ViewPager默认的翻页动画。
        // bannerView.setPageTransformer(true, new ScrollPaintingPageTransformer());
        bannerView.setOnPageClickListener(new BannerView.OnPageClickListener() {
            @Override
            public void onPageClick(BannerEntry entry, int index) {
                //某个页面被单击后执行，entry就是这个页面的数据模型。index是页面索引，从0开始。
                Log.e(TAG,"entry"+entry.toString()+'\n'+"被点击 index ："+(index+1));
                ImageBannerEntry im =(ImageBannerEntry) entry;
                Intent intent= new Intent();
                intent.setAction(Intent.ACTION_VIEW); //intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse(im.getGoToUrl());
                intent.setData(content_url);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
        //getImageBannerEntries()方法是从网络上获取数据。这里只是伪代码。
        //设置数据源并开始轮播。如果不希望启动轮播则调用两个参数的方法：bannerView.setEntries(bannerEntries, false); 你也可以通过bannerView.start();的方式启动轮播。
        bannerView.setEntries(getImageBannerEntries());
    }
    @NonNull
    private List<ImageBannerEntry> getImageBannerEntries() {
        List<ImageBannerEntry> items = new ArrayList<>();
        items.add(new ImageBannerEntry("奇秀好音乐，你身边的音乐真人秀", null, "http://pic2.qiyipic.com/common/20171027/cdc6210c26e24f08940d36a5eb918c34.jpg",getString(R.string.my_github_url)));

        items.add(new ImageBannerEntry("星空海2：陆漓设局害惨吴居蓝", "更新至30集", "http://m.qiyipic.com/common/lego/20171023/bd84e15d8dd44d7c9674218de30ac75c.jpg",getString(R.string.my_github_url)));
        items.add(new ImageBannerEntry("中国职业脱口秀大赛：狂笑首播", "10-28期", "http://m.qiyipic.com/common/lego/20171028/f1b872de43e649ddbf624b1451ebf95e.jpg",getString(R.string.my_github_url)));

        items.add(new ImageBannerEntry(null, null, "http://www.chinaags.net/image/catalog/%E9%A6%96%E9%A1%B5%E5%B9%BB%E7%81%AF%E7%89%872.jpg",getString(R.string.my_github_url)));
        items.add(new ImageBannerEntry(null, null, "http://www.chinaags.net/image/catalog/%E5%BA%97%E9%93%BA%E5%85%AC%E5%91%8A.jpg",getString(R.string.my_github_url)));
        return items;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MAINACTIVITY_HANDLER.removeCallbacksAndMessages(null);
    }
    private void GetUpdata(){
        try {
            String updata_url=getString(R.string.updata_url);
            //new UpdataAsyncTask(this).execute(updata_url);// 自动更新会造成，获取数据源获取延迟
            UpdataThread.getInstance(this,updata_url).start();
        }catch (Exception e){
        }
    }
}
